package com.mjk.redis;

import com.mjk.api.dto.ApiDto;
import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.error.BizException;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.base.work.BaseWork;
import com.mjk.common.base.work.annotation.BeeWork;
import com.mjk.common.core.redis.datatype.*;
import com.mjk.common.core.redis.listener.annotation.BeeRedisListener;
import com.mjk.common.core.redis.listener.annotation.RedisKeyEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * redis 测试 用例
 */
@BeeWork(work = "redis")
public class RedisExample extends BaseWork {

    @Autowired
    private RedisTemplate redisTemplate;

    public BeeResponse redisDataSource(ApiDto dto){
        needParams(dto,"dataSource");
        Map<String,Integer> data = new HashMap<>();
        List<Object> set = new ArrayList<>();
        switch (dto.getDataSource()){
            case "string":
                RedisUtils.set(redisTemplate,"string","11");
                break;
            case "list":
                RedisListUtils.lpush(redisTemplate,"list",new String[]{"1212","11222"});
                break;
            case "set":
                set.add(1);
                set.add("10");
                RedisSetUtils.add(redisTemplate,"set",set);
                break;
            case "hash":
                RedisHashUtils.set(redisTemplate,"hash","name","11");
                break;
            case "sortset":
                data.put("name",1);
                RedisSortedSetUtils.set(redisTemplate,"sortset","1",1);
                break;
            default:
                throw new BizException("datasource type is error");

        }
        return BeeError.BEE_SUC.tranceError();
    }


    @BeeRedisListener(evnet = RedisKeyEvent.UPDATE)
    public void redisListener(String key){
        //System.out.println(key);
    }

}
